import {
  getGoodsList,
  searchGoodList,
  addGoodList,
  delGoodList,
  getIdGoods,
  updateGoodList,
  totalNumGoods,
} from "@/api"; //配置好axios的大项目
//数据
const state = {
  // 物品发布页展示的数据
  goodsList: null,
  // 物品添加接口返回的提示
  addGoodMsg: null,
  // 物品删除接口返回的提示
  delGoodMsg: null,
  // 根据id获取物品的数据
  IdGoods: null,
  // 更新物品功能信息
  updateGoodMsg: null,
  // 物品总数量
  totalGoods: null,
};
//mutions是唯一修改state的地方
const mutations = {
  // 页面刷新获取物品信息
  GETGOODSLISTS(state, data) {
    state.goodsList = data;
  },
  // 搜索功能获取物品信息
  GETSEARCHGOODSLISTS(state, data) {
    state.goodsList = data;
  },
  // 添加物品功能信息
  ADDGOODSLISTS(state, data) {
    state.addGoodMsg = data;
  },
  // 删除物品功能信息
  DELGOODSLISTS(state, data) {
    state.delGoodMsg = data;
  },
  // 根据id获取物品数据
  GETIDGOODSS(state, data) {
    state.IdGoods = data;
  },
  // 更新物品功能信息
  UPDATEGOODSS(state, data) {
    state.updateGoodMsg = data;
  },
  // 获取物品总数量
  TOTALNUMGOODSLISTS(state, data) {
    state.totalGoods = data;
  },
};
//action|用户处理派发action地方的，可以书写异步语句、自己逻辑地方
const actions = {
  // 判断获取物品信息是否成功
  async getGoodsLists({ commit }, queryInfo) {
    let result = await getGoodsList(queryInfo);
    if (result.meta.status == 200) {
      commit("GETGOODSLISTS", result.data);
    }
  },
  // 判断获取搜索物品是否成功
  async getSearchGoodsLists({ commit }, keyWord) {
    let result = await searchGoodList(keyWord);
    if (result.meta.status == 200) {
      commit("GETSEARCHGOODSLISTS", result.data);
    }
  },
  // 判断添加物品是否成功
  async addGoodsLists({ commit }, goodsForm) {
    let result = await addGoodList(goodsForm);
    //console.log("添加物品", result);
    if (result.meta.status == 200) {
      commit("ADDGOODSLISTS", result.data);
    }
  },
  // 判断删除物品是否成功
  async delGoodsLists({ commit }, id) {
    let result = await delGoodList(id);
    if (result.meta.status == 200) {
      commit("DELGOODSLISTS", result.data);
    }
  },
  // 判断根据id获取物品数据是否成功
  async getIdGoodss({ commit }, id) {
    let result = await getIdGoods(id);
    if (result.meta.status == 200) {
      commit("GETIDGOODSS", result.data);
    }
  },
  // 判断更新物品是否成功
  async updateGoodss({ commit }, goodsForm) {
    let result = await updateGoodList(goodsForm);
    if (result.meta.status == 200) {
      commit("UPDATEGOODSS", result.data);
    }
  },
  // 判断获取物品数量是否成功
  async totalNumGoodsLists({ commit }) {
    let result = await totalNumGoods();
    if (result.meta.status == 200) {
      commit("TOTALNUMGOODSLISTS", result.data);
    }
  },
};
//计算属性
const getters = {};
export default {
  namespaced: true,
  state,
  mutations,
  actions,
  getters,
};
